local Msg = require("core.Msg")
local blueExcel = require("excel.Yellow")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Buffer   = require("buffer.Buffer")
local ItemDefine = require("bag.ItemDefine")
local PfLogic = require("tencentApi.PfLogic")
local InnerDataDefine = require("innerdata.Define")
local InnerDataManager = require("innerdata.InnerDataManagerFL")



--领取奖励
function getReward(human, type, index)
  if human.db.blueVip ~= 1 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_CANNOT_GET_REWARD)
  end
  if type == 1 then
    if human.db.blueNewReward == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if not BagLogic.checkCanAddItemList(human, blueExcel.blueNew[1].reward, #blueExcel.blueNew[1].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_BAG_FULL)
    end
    human.db.blueNewReward = ItemDefine.REWARD_ALREADY_GET
    for i=1, #blueExcel.blueNew[1].reward do
      BagLogic.addItem(human, blueExcel.blueNew[1].reward[i][1], 1, blueExcel.blueNew[1].reward[i][2], "blue")
    end    
  elseif type == 2 then
    if human.db.blueLvReward[index - 1] == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if human.db.lv < blueExcel.blueLv[index].lv then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_HUMAN_LV_NOT_ENOUGH)
    end
    if not BagLogic.checkCanAddItemList(human, blueExcel.blueLv[index].reward, #blueExcel.blueLv[index].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_BAG_FULL)
    end
    human.db.blueLvReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
    for i=1, #blueExcel.blueLv[index].reward do
      BagLogic.addItem(human, blueExcel.blueLv[index].reward[i][1], 1, blueExcel.blueLv[index].reward[i][2], "blue")
    end    
  elseif type == 3 then
    if human.db.blueDailyReward[index - 1] == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if index == 1 then
      if not BagLogic.checkCanAddItemList(human, blueExcel.blueDaily[human.db.blueVipLv].reward, #blueExcel.blueDaily[human.db.blueVipLv].reward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_BAG_FULL)
      end
      human.db.blueDailyReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
      for i=1, #blueExcel.blueDaily[human.db.blueVipLv].reward do
        BagLogic.addItem(human, blueExcel.blueDaily[human.db.blueVipLv].reward[i][1], 1, blueExcel.blueDaily[human.db.blueVipLv].reward[i][2], "blue")
      end
    elseif index == 2 then
      if human.db.blueYearVip ~= 1 then
        return
      end
      if not BagLogic.checkCanAddItemList(human, blueExcel.blueDaily[human.db.blueVipLv].yearReward, #blueExcel.blueDaily[human.db.blueVipLv].yearReward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_BAG_FULL)
      end
      human.db.blueDailyReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
      for i=1, #blueExcel.blueDaily[1].yearReward do
        BagLogic.addItem(human, blueExcel.blueDaily[1].yearReward[i][1], 1, blueExcel.blueDaily[1].yearReward[i][2], "blue")
      end
    elseif index == 3 then
      if human.db.blueSuperVip ~= 1 then
        return
      end
      if not BagLogic.checkCanAddItemList(human, blueExcel.blueDaily[1].superReward, #blueExcel.blueDaily[1].superReward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.BLUE_BAG_FULL)
      end
      human.db.blueDailyReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
      for i=1, #blueExcel.blueDaily[1].superReward do
        BagLogic.addItem(human, blueExcel.blueDaily[1].yearReward[i][1], 1, blueExcel.blueDaily[1].superReward[i][2], "blue")
      end
    end   
  else
    return
  end
  sendRewardStatus(human, type)
end
--发送奖励列表
function sendRewardList(human, type)
  local sendMsg = Msg.gc.GC_BLUE_REWARD_LIST
  local cache = {}
  sendMsg.type = type
  if type == 1 then
    sendMsg.rewardList[0] = #blueExcel.blueNew
    sendMsg.rewardList[1].condition = 0
    sendMsg.rewardList[1].reward[0] = #blueExcel.blueNew[1].reward
    for i=1, sendMsg.rewardList[1].reward[0] do
      Grid.create(cache, blueExcel.blueNew[1].reward[i][1], blueExcel.blueNew[1].reward[i][2])
      Grid.makeItem(cache, sendMsg.rewardList[1].reward[i], 0, human)
    end
    sendMsg.rewardList[1].attr[0] = 0
  elseif type == 2 then
    sendMsg.rewardList[0] = #blueExcel.blueLv
    for i=1,sendMsg.rewardList[0] do
      sendMsg.rewardList[i].condition = blueExcel.blueLv[i].lv
      sendMsg.rewardList[i].reward[0] = #blueExcel.blueLv[i].reward
      for j=1, sendMsg.rewardList[i].reward[0] do
        Grid.create(cache, blueExcel.blueLv[i].reward[j][1], blueExcel.blueLv[i].reward[j][2])
        Grid.makeItem(cache, sendMsg.rewardList[i].reward[j], 0, human)
      end 
      sendMsg.rewardList[i].attr[0] = 0    
    end 
  elseif type == 3 then
    sendMsg.rewardList[0] = #blueExcel.blueDaily + 2
    for i=1,#blueExcel.blueDaily do
      sendMsg.rewardList[i].condition = 0
      sendMsg.rewardList[i].reward[0] = #blueExcel.blueDaily[i].reward
      for j=1, sendMsg.rewardList[i].reward[0] do
        Grid.create(cache, blueExcel.blueDaily[i].reward[j][1], blueExcel.blueDaily[i].reward[j][2])
        Grid.makeItem(cache, sendMsg.rewardList[i].reward[j], 0, human)
      end
      sendMsg.rewardList[i].attr[0] = 0    
    end
    sendMsg.rewardList[sendMsg.rewardList[0] - 1].reward[0] = #blueExcel.blueDaily[1].yearReward
    for i=1, #blueExcel.blueDaily[1].yearReward do
      Grid.create(cache, blueExcel.blueDaily[1].yearReward[i][1], blueExcel.blueDaily[1].yearReward[i][2])
      Grid.makeItem(cache, sendMsg.rewardList[sendMsg.rewardList[0] - 1].reward[i], 0, human)      
    end
    sendMsg.rewardList[sendMsg.rewardList[0]].reward[0] = #blueExcel.blueDaily[1].superReward
    for i=1, #blueExcel.blueDaily[1].superReward do
      Grid.create(cache, blueExcel.blueDaily[1].superReward[i][1], blueExcel.blueDaily[1].superReward[i][2])
      Grid.makeItem(cache, sendMsg.rewardList[sendMsg.rewardList[0]].reward[i], 0, human)      
    end
    sendMsg.rewardList[sendMsg.rewardList[0] - 1].condition = 0
    sendMsg.rewardList[sendMsg.rewardList[0] - 1].attr[0] = 0 
    sendMsg.rewardList[sendMsg.rewardList[0] ].condition = 0
    sendMsg.rewardList[sendMsg.rewardList[0] ].attr[0] = 0 
  else
    sendMsg.rewardList[0] = #blueExcel.blueOther
    for i=1,sendMsg.rewardList[0] do
      sendMsg.rewardList[i].condition = 0
      sendMsg.rewardList[i].reward[0] = 0
      sendMsg.rewardList[i].attr[0] = #blueExcel.blueOther[i].attr
      for j=1, sendMsg.rewardList[i].attr[0] do
        sendMsg.rewardList[i].attr[j].key = blueExcel.blueOther[i].attr[j][1]
        sendMsg.rewardList[i].attr[j].value = blueExcel.blueOther[i].attr[j][2]
      end
    end 
  end
  Msg.send(sendMsg, human.fd)
  sendRewardStatus(human, type)
end
--发送状态
function sendRewardStatus(human, type)
  local sendMsg = Msg.gc.GC_BLUE_REWARD_STATUS
  sendMsg.type = type
  if type == 1 then
    sendMsg.status[0] = #blueExcel.blueNew
    sendMsg.status[1] = human.db.blueNewReward
  elseif type == 2 then
    sendMsg.status[0] = #blueExcel.blueLv
    for i=1, sendMsg.status[0] do
      sendMsg.status[i] = human.db.blueLvReward[i-1]
    end    
  elseif type == 3 then
    sendMsg.status[0] = 3
    for i=1, sendMsg.status[0] do
      sendMsg.status[i] = human.db.blueDailyReward[i-1]
    end
  else
    return
  end
  Msg.send(sendMsg, human.fd)
end

function addBuffer(human)
  if human.db.blueVip ~= 0 and PfLogic.isBlueShow(human) then
    for i=1, #blueExcel.blueOther[1].buf do
      Buffer.add(blueExcel.blueOther[1].buf[i], human, human)
    end
  end
end

function clearStatus(human)
  human.db.blueDailyReward[0] = 0
  human.db.blueDailyReward[1] = 0
  human.db.blueDailyReward[2] = 0
  for i=1,3 do
    sendRewardStatus(human, i)
  end
end

function onLoginBlueInfo(human, type, data)
  addBuffer(human)
end
InnerDataManager.Register(InnerDataDefine.INNERDATA_TYPE_BLUE_ADD,  onLoginBlueInfo)
